#include "stdafx.h"
#include "LangBarItemButton.h"


CLangBarItemButton::CLangBarItemButton(CTextService* pTextService)
{
	DebugPrintfW(_TEXT("CLangBarItemButton::CLangBarItemButton\n"));
	 _pTextService = pTextService;
	 DllAddRef();

    //
    // initialize TF_LANGBARITEMINFO structure.
    //
    _tfLangBarItemInfo.clsidService = c_clsidTextService;    // This LangBarItem belongs to this TextService.
    _tfLangBarItemInfo.guidItem = c_guidLangBarItemButton;   // GUID of this LangBarItem.
    _tfLangBarItemInfo.dwStyle = TF_LBI_STYLE_BTN_MENU;      // This LangBar is a button type with a menu.
    _tfLangBarItemInfo.ulSort = 0;                           // The position of this LangBar Item is not specified.
    StringCbCopy(_tfLangBarItemInfo.szDescription, IME_ARRAYSIZE(_tfLangBarItemInfo.szDescription), LANGBAR_ITEM_DESC);                        // Set the description of this LangBar Item.

    // Initialize the sink pointer to NULL.
    _pLangBarItemSink = NULL;

    _cRef = 1;
}


CLangBarItemButton::~CLangBarItemButton(void)
{
	DebugPrintfW(_TEXT("CLangBarItemButton::~CLangBarItemButton\n"));
	DllRelease();
}

//+---------------------------------------------------------------------------
//
// QueryInterface
//
//----------------------------------------------------------------------------

STDAPI CLangBarItemButton::QueryInterface(REFIID riid, void **ppvObj)
{
	DebugPrintfW(_TEXT("CLangBarItemButton::QueryInterface\n"));
    if (ppvObj == NULL)
        return E_INVALIDARG;

    *ppvObj = NULL;

    if (IsEqualIID(riid, IID_IUnknown) ||
        IsEqualIID(riid, IID_ITfLangBarItem) ||
        IsEqualIID(riid, IID_ITfLangBarItemButton))
    {
        *ppvObj = (ITfLangBarItemButton *)this;
    }
    else if (IsEqualIID(riid, IID_ITfSource))
    {
        *ppvObj = (ITfSource *)this;
    }

    if (*ppvObj)
    {
        AddRef();
        return S_OK;
    }

    return E_NOINTERFACE;
}


//+---------------------------------------------------------------------------
//
// AddRef
//
//----------------------------------------------------------------------------

STDAPI_(ULONG) CLangBarItemButton::AddRef()
{
	DebugPrintfW(_TEXT("CLangBarItemButton::AddRef\n"));
    return ++_cRef;
}

//+---------------------------------------------------------------------------
//
// Release
//
//----------------------------------------------------------------------------

STDAPI_(ULONG) CLangBarItemButton::Release()
{
	DebugPrintfW(_TEXT("CLangBarItemButton::Release\n"));
    LONG cr = --_cRef;

    assert(_cRef >= 0);

    if (_cRef == 0)
    {
        delete this;
    }

    return cr;
}

//+---------------------------------------------------------------------------
//
// GetInfo
//
//----------------------------------------------------------------------------

STDAPI CLangBarItemButton::GetInfo(TF_LANGBARITEMINFO *pInfo)
{
	DebugPrintfW(_TEXT("CLangBarItemButton::GetInfo\n"));
    *pInfo = _tfLangBarItemInfo;
    return S_OK;
}

//+---------------------------------------------------------------------------
//
// GetStatus
//
//----------------------------------------------------------------------------

STDAPI CLangBarItemButton::GetStatus(DWORD *pdwStatus)
{
	DebugPrintfW(_TEXT("CLangBarItemButton::GetStatus\n"));
    *pdwStatus = 0;
    return S_OK;
}

//+---------------------------------------------------------------------------
//
// Show
//
//----------------------------------------------------------------------------

STDAPI CLangBarItemButton::Show(BOOL fShow)
{
	DebugPrintfW(_TEXT("CLangBarItemButton::Show\n"));
    return E_NOTIMPL;
}

//+---------------------------------------------------------------------------
//
// GetTooltipString
//
//----------------------------------------------------------------------------

STDAPI CLangBarItemButton::GetTooltipString(BSTR *pbstrToolTip)
{
	DebugPrintfW(_TEXT("CLangBarItemButton::GetTooltipString\n"));
    *pbstrToolTip = SysAllocString(LANGBAR_ITEM_DESC);

    return (*pbstrToolTip == NULL) ? E_OUTOFMEMORY : S_OK;
}

//+---------------------------------------------------------------------------
//
// OnClick
//
//----------------------------------------------------------------------------

STDAPI CLangBarItemButton::OnClick(TfLBIClick click, POINT pt, const RECT *prcArea)
{
	DebugPrintfW(_TEXT("CLangBarItemButton::OnClick\n"));
    return S_OK;
}

//+---------------------------------------------------------------------------
//
// InitMenu
//
//----------------------------------------------------------------------------

STDAPI CLangBarItemButton::InitMenu(ITfMenu *pMenu)
{
	DebugPrintfW(_TEXT("CLangBarItemButton::InitMenu\n"));
    // 
    // Add the fisrt menu item.
    // 
    pMenu->AddMenuItem(MENUITEM_INDEX_0,
                       0, 
                       NULL, 
                       NULL, 
                       c_szMenuItemDescription0, 
                       (ULONG)wcslen(c_szMenuItemDescription0), 
                       NULL);

    // 
    // Add the second menu item.
    // 
    pMenu->AddMenuItem(MENUITEM_INDEX_1,
                       0, 
                       NULL, 
                       NULL, 
                       c_szMenuItemDescription1, 
                       (ULONG)wcslen(c_szMenuItemDescription1), 
                       NULL);
	 // 
    // Add the insert Hello menu item.
    // 
    pMenu->AddMenuItem(MENUITEM_INDEX_INSERTHELLO,
                       0, 
                       NULL, 
                       NULL, 
                       c_szMenuItemDescriptionInsertHello, 
                       (ULONG)wcslen(c_szMenuItemDescriptionInsertHello), 
                       NULL);

	// 
    // Add the keyboard open close item.
    // 
    DWORD dwFlags = 0;
    if (_pTextService->_IsKeyboardDisabled())
        dwFlags |= TF_LBMENUF_GRAYED;
    else if (_pTextService->_IsKeyboardOpen())
        dwFlags |= TF_LBMENUF_CHECKED;

    pMenu->AddMenuItem(MENUITEM_INDEX_OPENCLOSE,
                       dwFlags, 
                       NULL, 
                       NULL, 
                       c_szMenuItemDescriptionOpenClose, 
                       (ULONG)wcslen(c_szMenuItemDescriptionOpenClose), 
                       NULL);
    return S_OK;
}

//+---------------------------------------------------------------------------
//
// OnMenuSelect
//
//----------------------------------------------------------------------------

STDAPI CLangBarItemButton::OnMenuSelect(UINT wID)
{
	DebugPrintfW(_TEXT("CLangBarItemButton::OnMenuSelect\n"));
    //
    // This is callback when the menu item is selected.
    //
	BOOL fOpen;
    switch (wID)
    {
        case MENUITEM_INDEX_0:
			DebugPrintfW(_TEXT("You select menu item 0"));
            break;

        case MENUITEM_INDEX_1:
			DebugPrintfW(_TEXT("You select menu item 1"));
            break;
		 case MENUITEM_INDEX_INSERTHELLO:
			 DebugPrintfW(_TEXT("You select menu item 'INSERT HELLO' "));
            //_pTextService->InsertHello();
            break;
		  case MENUITEM_INDEX_OPENCLOSE:
            fOpen = _pTextService->_IsKeyboardOpen();
            _pTextService->_SetKeyboardOpen(fOpen ? FALSE : TRUE);
            break;
    }

    return S_OK;
}

//+---------------------------------------------------------------------------
//
// GetIcon
//
//----------------------------------------------------------------------------

STDAPI CLangBarItemButton::GetIcon(HICON *phIcon)
{
	DebugPrintfW(_TEXT("CLangBarItemButton::GetIcon\n"));
    *phIcon = (HICON)LoadImage(g_hInst, TEXT("IDI_TEXTSERVICE"), IMAGE_ICON, 16, 16, 0);
 
    return (*phIcon != NULL) ? S_OK : E_FAIL;
}

//+---------------------------------------------------------------------------
//
// GetText
//
//----------------------------------------------------------------------------

STDAPI CLangBarItemButton::GetText(BSTR *pbstrText)
{
	DebugPrintfW(_TEXT("CLangBarItemButton::GetText\n"));
    *pbstrText = SysAllocString(LANGBAR_ITEM_DESC);

    return (*pbstrText == NULL) ? E_OUTOFMEMORY : S_OK;
}

//+---------------------------------------------------------------------------
//
// AdviseSink
//
//----------------------------------------------------------------------------

STDAPI CLangBarItemButton::AdviseSink(REFIID riid, IUnknown *punk, DWORD *pdwCookie)
{
	DebugPrintfW(_TEXT("CLangBarItemButton::AdviseSink\n"));
    //
    // Allow only ITfLangBarItemSink interface.
    //
    if (!IsEqualIID(IID_ITfLangBarItemSink, riid))
        return CONNECT_E_CANNOTCONNECT;

    //
    // support only one sink.
    //
    if (_pLangBarItemSink != NULL)
        return CONNECT_E_ADVISELIMIT;

    //
    // Query the ITfLangBarItemSink interface and store it into _pLangBarItemSink.
    //
    if (punk->QueryInterface(IID_ITfLangBarItemSink, (void **)&_pLangBarItemSink) != S_OK)
    {
        _pLangBarItemSink = NULL;
        return E_NOINTERFACE;
    }

    //
    // return our cookie.
    //
    *pdwCookie = TEXTSERVICE_LANGBARITEMSINK_COOKIE;
    return S_OK;
}

//+---------------------------------------------------------------------------
//
// UnadviseSink
//
//----------------------------------------------------------------------------

STDAPI CLangBarItemButton::UnadviseSink(DWORD dwCookie)
{
	DebugPrintfW(_TEXT("CLangBarItemButton::UnadviseSink\n"));
    // 
    // Check the given cookie.
    // 
    if (dwCookie != TEXTSERVICE_LANGBARITEMSINK_COOKIE)
        return CONNECT_E_NOCONNECTION;

    //
    // If there is no connected sink, just fail.
    //
    if (_pLangBarItemSink == NULL)
        return CONNECT_E_NOCONNECTION;

    _pLangBarItemSink->Release();
    _pLangBarItemSink = NULL;

    return S_OK;
}

